package cn.oms.oms.service.impl;

import cn.oms.oms.entity.Bill;
import cn.oms.oms.entity.Department;
import cn.oms.oms.entity.Patient;
import cn.oms.oms.exception.ServiceException;
import cn.oms.oms.mapper.DepartmentMapper;
import cn.oms.oms.service.IDepartmentService;
import cn.oms.oms.vo.DepartmentVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AllenChen曾晨
 * @since 2022-04-29
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 根据查询条件查询科室列表
     * @param departmentVo
     * @return
     */
    @Override
    public PageInfo<Department> selectDepartmentList(DepartmentVo departmentVo) {

        QueryWrapper<Department> query = new QueryWrapper<>();

        // 查询 模糊查询条件设置
        if (StringUtil.isNotEmpty(departmentVo.getDepartmentName())){
            query.like("specialtyName",departmentVo.getDepartmentName()).or().like("departmentName",departmentVo.getDepartmentName());
        }
        query.eq("deleteFlag",0);

        if (departmentVo.getPharmacyFlag()!=null&&!"".equals(departmentVo.getPharmacyFlag())){
            query.eq("pharmacyFlag",departmentVo.getPharmacyFlag());
        }

        PageHelper.startPage(departmentVo.getPageNum(),departmentVo.getPageSize());

        List<Department> departmentList = departmentMapper.selectList(query);

        // 时间格式化
        for (Department department: departmentList) {
            // 创建时间格式化
            String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(department.getCreateTime());
            department.setReturnCreateTime(date);
        }

        return new PageInfo<>(departmentList);
    }

    /**
     * 查询科室详情
     * @param departmentVo
     * @return
     */
    @Override
    public Department selectDepartmentDetail(DepartmentVo departmentVo) {
        if (departmentVo.getDepartmentId()==null&&"".equals(departmentVo.getDepartmentId())){
            throw new ServiceException("参数异常，未知的科室");
        }
        QueryWrapper<Department> query = new QueryWrapper<>();
        query.eq("id",departmentVo.getDepartmentId());

        Department department = departmentMapper.selectOne(query);
        if (department==null){
            throw new ServiceException("未查询到科室");
        }

        if (department.getPharmacyFlag()==0){
            department.setPharmacyFlagName("非药房");
        }
        if (department.getPharmacyFlag()==1){
            department.setPharmacyFlagName("药房");
        }

        return department;
    }

    /**
     * 新增科室
     * @param departmentVo
     * @return
     */
    @Override
    public String addDepartment(DepartmentVo departmentVo) {

        Department department = new Department();
        department.setSpecialtyName(departmentVo.getSpecialtyName());
        department.setDepartmentName(departmentVo.getDepartmentName());
        department.setDepartmentAddress(departmentVo.getDepartmentAddress());
        department.setPharmacyFlag(departmentVo.getPharmacyFlag());
        department.setDepartmentDoctorNum(0);
        department.setDeleteFlag(0);
        department.setCreateTime(new Date());
        int num = departmentMapper.insert(department);
        if (num!=1){
            throw new ServiceException("数据库异常");
        }
        return "ok";
    }

    /**
     * 修改科室
     * @param departmentVo
     * @return
     */
    @Override
    public String updateDepartment(DepartmentVo departmentVo) {
        if (departmentVo.getDepartmentId()==null||"".equals(departmentVo.getDepartmentId())){
            throw new ServiceException("参数异常，修改失败");
        }
        QueryWrapper<Department> query = new QueryWrapper<>();
        query.eq("id",departmentVo.getDepartmentId());
        query.eq("deleteFlag",0);
        Department department = departmentMapper.selectOne(query);

        if (department==null){
            throw new ServiceException("科室不存在");
        }
        if (StringUtil.isNotEmpty(departmentVo.getSpecialtyName())){
            department.setSpecialtyName(departmentVo.getSpecialtyName());
        }
        if (StringUtil.isNotEmpty(departmentVo.getDepartmentName())){
            department.setDepartmentName(departmentVo.getDepartmentName());
        }
        if (StringUtil.isNotEmpty(departmentVo.getDepartmentAddress())){
            department.setDepartmentAddress(departmentVo.getDepartmentAddress());
        }
        if (departmentVo.getPharmacyFlag()!=null&&!"".equals(departmentVo.getPharmacyFlag())){
            department.setPharmacyFlag(departmentVo.getPharmacyFlag());
        }

        int num = departmentMapper.updateById(department);
        if (num!=1){
            throw new ServiceException("数据库异常");
        }
        return "ok";
    }

    /**
     * 删除科室
     * @param id
     * @return
     */
    @Override
    public String deleteDepartment(Integer id) {

        if (id==null&&"".equals(id)){
            throw new ServiceException("参数异常，科室ID为空");
        }
        QueryWrapper<Department> query = new QueryWrapper<>();
        query.eq("id",id);
        query.eq("deleteFlag",0);
        Department department = departmentMapper.selectOne(query);
        if (department==null){
            throw new ServiceException("科室不存在");
        }
        department.setDeleteFlag(1);
        int num = departmentMapper.updateById(department);

        if (num!=1){
            throw new ServiceException("数据库异常");
        }
        return "ok";
    }
}
